package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.constant.HttpStatus;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.dto.PageDto;
import com.zzyl.nursing.dto.ReservationPageDto;
import com.zzyl.nursing.vo.CountByTimeMemberVo;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 预约信息Service业务层处理
 *
 * @author alexis
 * @date 2025-01-12
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     *
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id)
    {
        return reservationMapper.selectById(id);
    }

    /**
     * 查询预约信息列表
     *
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList( Reservation reservation)
    {
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约信息
     *
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation)
    {
        return reservationMapper.insert(reservation);
    }

    /**
     * 修改预约信息
     *
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation)
    {
        return reservationMapper.updateById(reservation);
    }

    /**
     * 批量删除预约信息
     *
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids)
    {
        return reservationMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除预约信息信息
     *
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id)
    {
        return reservationMapper.deleteById(id);
    }

    @Override
    public Integer getCancelledReservationCount(Long userId) {
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        Integer count = (int) count(Wrappers.<Reservation>lambdaQuery().eq(Reservation::getUpdateBy, userId)
                .eq(Reservation::getStatus, 2)
                .between(Reservation::getUpdateTime, startTime, endTime));
        return count;
    }

    @Override
    public List<CountByTimeMemberVo> getCountByTime(Long time) {
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        List<CountByTimeMemberVo> countByTimeMemberVoList = reservationMapper.getCountByTime(startTime, endTime);
        return countByTimeMemberVoList;
    }

    @Override
    public void addReservation(Reservation reservation) {
        Long userId = UserThreadLocal.getUserId();
        reservation.setStatus(0);
        reservation.setCreateBy(String.valueOf(userId));
        reservation.setUpdateBy(String.valueOf(userId));
        reservationMapper.insert(reservation);
    }

    @Override
    public TableDataInfo<Reservation> reservationPage(Integer pageNum, Integer pageSize, Integer status) {
        Long userId = UserThreadLocal.getUserId();
        Page<Reservation> page=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Reservation> wrapper = Wrappers.<Reservation>lambdaQuery()
                .eq(Reservation::getStatus, status)
                .eq(Reservation::getCreateBy, userId)
                .orderByDesc(Reservation::getCreateTime);
        page= page(page, wrapper);
        return getTableDataInfo(page);
    }

    private TableDataInfo<Reservation> getTableDataInfo(Page<Reservation> page) {
        TableDataInfo<Reservation> tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setRows(page.getRecords());
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        return tableDataInfo;
    }

    @Override
    public void cancelReservation(Long id) {
        Long userId = UserThreadLocal.getUserId();
        Reservation reservation = getById(id);
        if (reservation == null){
            throw new RuntimeException("预约不存在");
        }
        reservation.setStatus(2);
        reservation.setUpdateBy(String.valueOf(userId));
        updateById(reservation);
    }

    @Override
    public void updateReservationStatus() {
        List<Reservation> reservationList = list(Wrappers.<Reservation>lambdaQuery()
                .lt(Reservation::getTime, LocalDateTime.now().minusMinutes(30))
                .eq(Reservation::getStatus, 0)
        );
        reservationList.forEach(reservation -> reservation.setStatus(3));
        updateBatchById(reservationList);
    }
}
